import java.util.HashMap;

/**
 * @author admin
 *383. 赎金信
 * //解法：用两个HashMap统计两个字符串中的字符数，然后对第一个字符串遍历，条件为第二个字符串中字符含有第一个字符串中的字符并且数量要比第一个多
 */
public class LeeCode383 {

    public boolean canConstruct(String ransomNote, String magazine) {

        HashMap<Character, Integer> map1 = new HashMap<>();
        HashMap<Character, Integer> map2 = new HashMap<>();

        for (int i = 0 ;i < ransomNote.length();i++){
            map1.put(ransomNote.charAt(i),map1.getOrDefault(ransomNote.charAt(i),0)+1);
        }

        for (int i = 0 ;i < magazine.length();i++){
            map2.put(magazine.charAt(i),map2.getOrDefault(magazine.charAt(i),0)+1);
        }

        for (Character character : map1.keySet()) {
            if(!map2.containsKey(character) || map1.get(character) > map2.get(character)){
                return false;
            }
        }

        return true;

    }

    public static void main(String[] args) {

        String ransom = "aa";
        String magazine = "ab";
        System.out.println(new LeeCode383().canConstruct(ransom, magazine));
    }
}
